﻿namespace PayPal.ButtonManager
{
    using System;
    using System.Collections.Generic;

    using com.paypal.sdk.services;
    using com.paypal.sdk.util;

    public static class AddToCartButton
    {
        public static ButtonManagerResponse Create(string business, string itemName, string amount)
        {
            HtmlButtonVariables buttonVariables = new HtmlButtonVariables()
            {
                Business = business,
                ItemName = itemName,
                Amount = amount,
            };

            return Create(buttonVariables);
        }

        public static ButtonManagerResponse Create(
                            HtmlButtonVariables buttonVariables,
                            ButtonOption[] buttonOptions = null,
                            string buttonImage = "",
                            string buttonImageUrl = "",
                            string buttonSubType = "",
                            string buttonCountry = "",
                            string buttonLanguage = "")
        {
            return Create(HtmlButtonVariablesHelper.GetProperties(buttonVariables), buttonOptions, buttonImage, buttonImageUrl, buttonSubType, buttonCountry, buttonLanguage);
        }

        public static ButtonManagerResponse Create(
                            Dictionary<string, string> buttonVariables,
                            ButtonOption[] buttonOptions = null,
                            string buttonImage = "",
                            string buttonImageUrl = "",
                            string buttonSubType = "",
                            string buttonCountry = "",
                            string buttonLanguage = "")
        {
            NVPCallerServices caller = new NVPCallerServices();
            caller.APIProfile = Profile.BMProfile;

            NVPCodec encoder = NameValuePairApiHelper.PrepareEncoder(
                                            method: "BMCreateButton", 
                                            buttonCode: "HOSTED", 
                                            buttonType: "CART", 
                                            buttonSubType: buttonSubType,
                                            buttonImage: buttonImage, 
                                            buttonImageUrl: buttonImageUrl,
                                            buttonCountry: buttonCountry, 
                                            buttonLanguage: buttonLanguage);

            NameValuePairApiHelper.PrepareButtonVars(encoder, buttonVariables, buttonOptions);
            
            string response = caller.Call(encoder.Encode());

            NVPCodec decoder = new NVPCodec();
            decoder.Decode(response);                       

            if (decoder.Get("ACK").Equals("Failure", StringComparison.InvariantCultureIgnoreCase))
            {
                throw new ButtonManagerException(decoder.Get("L_ERRORCODE0"), decoder.Get("L_SHORTMESSAGE0"), decoder.Get("L_LONGMESSAGE0"));
            }

            return NameValuePairApiHelper.PrepareResponse(decoder);
        }

        public static ButtonManagerResponse Update(
                string hostedButtonId,
                string business,
                string itemName,
                string amount)
        {
            HtmlButtonVariables buttonVariables = new HtmlButtonVariables()
            {
                Business = business,
                ItemName = itemName,
                Amount = amount,
            };

            return Update(hostedButtonId, HtmlButtonVariablesHelper.GetProperties(buttonVariables));
        }

        public static ButtonManagerResponse Update(
                            string hostedButtonId,
                            HtmlButtonVariables buttonVariables,
                            ButtonOption[] buttonOptions = null,
                            string buttonImage = "",
                            string buttonImageUrl = "",
                            string buttonSubType = "",
                            string buttonCountry = "",
                            string buttonLanguage = "")
        {
            return Update(hostedButtonId, HtmlButtonVariablesHelper.GetProperties(buttonVariables), buttonOptions, buttonImage, buttonImageUrl, buttonSubType, buttonCountry, buttonLanguage);
        }

        public static ButtonManagerResponse Update(
                            string hostedButtonId,
                            Dictionary<string, string> vars,
                            ButtonOption[] opts = null,
                            string buttonImage = "",
                            string buttonImageUrl = "",
                            string buttonSubType = "",
                            string buttonCountry = "",
                            string buttonLanguage = "")
        {
            NVPCallerServices caller = new NVPCallerServices();
            caller.APIProfile = Profile.BMProfile;

            NVPCodec encoder = NameValuePairApiHelper.PrepareEncoder(
                                            method: "BMUpdateButton",
                                            buttonCode: "HOSTED",
                                            buttonType: "CART",
                                            hostedButtonId: hostedButtonId,
                                            buttonSubType: buttonSubType,
                                            buttonImage: buttonImage,
                                            buttonImageUrl: buttonImageUrl,
                                            buttonCountry: buttonCountry,
                                            buttonLanguage: buttonLanguage);            

            NameValuePairApiHelper.PrepareButtonVars(encoder, vars, opts);
            
            string response = caller.Call(encoder.Encode());

            NVPCodec decoder = new NVPCodec();
            decoder.Decode(response);            

            if (decoder.Get("ACK").Equals("Failure", StringComparison.InvariantCultureIgnoreCase))
            {
                throw new ButtonManagerException(decoder.Get("L_ERRORCODE0"), decoder.Get("L_SHORTMESSAGE0"), decoder.Get("L_LONGMESSAGE0"));
            }

            return NameValuePairApiHelper.PrepareResponse(decoder);
        }

        public static void Delete(string hostedButtonId)
        {
            ButtonManagerHelper.DeleteButton(hostedButtonId);
        }
    }
}
